// pages/truckCenter/typeLength.js
import {
  $Message
} from '../../components/base/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    carLengthList: [
      { label: '不限车长', value: '0' },
      { label: '1.8米', value: '1.8' },
      { label: '2.7米', value: '2.7' },
      { label: '3.8米', value: '3.8' },
      { label: '4.2米', value: '4.2' },
      { label: '5米', value: '5' },
      { label: '6.2米', value: '6.2' },
      { label: '6.8米', value: '6.8' },
      { label: '7.7米', value: '7.7' },
      { label: '8.2米', value: '8.2' },
      { label: '8.7米', value: '8.7' },
      { label: '9.6米', value: '9.6' },
      { label: '11.7米', value: '11.7' },
      { label: '12.5米', value: '12.5' },
      { label: '13米', value: '13' },
      { label: '13.7米', value: '13.7' },
      { label: '15米', value: '15' },
      { label: '16米', value: '16' },
      { label: '17.5米', value: '17.5' },
    ],
    carTypeList: [
      '不限车型', '平板', '高栏', '厢式', '集装箱', '面包车',
      '棉被车', '爬梯车', '高低板', '飞翼车'
    ],
    truckTypeName: '',
    truckLength: '',
    lengthInput: '',
  },
  changeCarLength(event) {
    const truckLength = event.currentTarget.dataset.trucklength;
    this.setData({
      truckLength: truckLength,
      lengthInput: ''
    })
  },
  carLengthInputChange(event) {
    const lengthInput = event.detail.value;
    this.setData({
      truckLength: lengthInput,
      lengthInput: lengthInput
    })
  },
  changeCarType(event) {
    const carType = event.currentTarget.dataset.cartype;
    this.setData({
      truckTypeName: carType,
    })
  },
  submitTypeLength() {
    const typeLength = {
      truckLength: this.data.truckLength,
      truckTypeName: this.data.truckTypeName
    }
    if (this.checkTypeLength(typeLength)) {
      wx.setStorageSync('typeLength', typeLength);
      if(this.data.orderType==='truckCenter'){
        wx.navigateTo({
          url: '/pages/truckCenter/'+this.data.fromPage,
        })
      }else if(this.data.orderType==='orderCenter'){
        wx.navigateTo({
          url: '/pages/orderCenter/addv2',
        })
      }
    }
  },

  checkTypeLength(value){
    if(!value.truckLength){
      this.showErrorMessage('车长不符合要求');
      return false;
    }
    if(!value.truckTypeName){
      this.showErrorMessage('车型不符合要求');
      return false;
    }
    return true;

  },
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {orderType,fromPage} = options;
    this.setData({
      orderType,
      fromPage,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})